<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="search()" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" :model="formState">
        <a-row :gutter="24">
          <a-col :span="6">
            <a-form-item name="mcbh" label="">
              <a-input v-model:value="formState.mcbh" placeholder="诊断代码、诊断名称、助记码" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item name="state" label="">
              <a-radio-group v-model:value="formState.state">
                <a-radio value="0">启用</a-radio>
                <a-radio value="">全部</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3">
        <a-button type="primary" @click="onHandleEdit">新增</a-button>
        <!-- <a-button class="ml-2">导出</a-button> -->
      </div>
      <BaseTable
        column-code="00001051"
        fill
        :data-source="tableData"
        bordered
        :loading="loading"
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, record, index }">
          <template v-if="column.dataIndex == 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex == 'ztflag'">
            <div>{{ record.ztflag ? '已停用' : '启用' }}</div>
          </template>
          <template v-if="column.dataIndex == 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)">修改</a-button>
            <a-divider type="vertical" />
            <ConfirmModal description="确定删除?" @confirm="onHandleDelect(record)"> <a-button type="text" danger size="small"> 删除 </a-button> </ConfirmModal>
            <a-divider type="vertical" />
            <a-button type="link" size="small" :disabled="!record.ztflag" @click="onHandleDeactivate(record, 0)">启用</a-button>
            <a-divider type="vertical" />
            <ConfirmModal description="确定停用?" @confirm="onHandleDeactivate(record, 1)">
              <a-button type="text" danger size="small" :disabled="record.ztflag"> 停用 </a-button>
            </ConfirmModal>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" @confirm="editOpen = false" />
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import ConfirmModal from '@/components/ConfirmModal'
import { post } from '@/utils/request'
import Edit from './Edit'

const editOpen = ref(false)
const loading = ref(false)
const formRef = ref(null)
const rowData = ref(null)
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const formState = ref({
  mcbh: '',
  state: '0'
})
const tableData = ref(null)
const onHandleEdit = (row) => {
  editOpen.value = true
  rowData.value = { ...row }
}

const getData = async () => {
  loading.value = true
  const { code, data, total } = await post('operation/opex/getDiagnosisCodeInfoList', {
    mcbh: formState.value.mcbh,
    pageNum: page.value.current,
    pageSize: page.value.pageSize,
    ztflag: formState.value.state
  })
  loading.value = false
  if (code === 0) {
    page.value.total = total
    tableData.value = data
  }
}
const search = () => {
  getData()
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  getData()
}
const onHandleDeactivate = async (item, number) => {
  item.ztflag = number
  const { code, msg } = await post('operation/opex/doSaveDiagnosisCodeInfo', item)
  if (code === 0) {
    message.success(msg)
    getData()
  }
}

const onHandleDelect = async (item) => {
  const { code, msg } = await post('operation/opex/doDeleteDiagnosisCodeInfo', item)
  if (code === 0) {
    message.success(msg)
    getData()
  }
}
const onSuceess = () => {
  editOpen.value = false
  getData()
}
onMounted(() => {
  getData()
})
</script>
